package com.example.springboot_demo.schedule;

import com.example.springboot_demo.controller.FormController;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * <P> @Author: SongKeCheng
 * <P> Type: Class
 * <P> Description:spring 和 springBoot的生命周期 非懒加载单例bean的生命周期
 * <P> Date: 2024/6/23:15:53
 */

@SuppressWarnings({"unchecked", ""})
//@Component
@Slf4j
//@Lazy
public class BeanLif implements InitializingBean,
        SmartInitializingSingleton , ApplicationRunner, BeanFactoryAware {


    public BeanLif() {
        log.error("执行构造器");
    }

    @Autowired
    private FormController formController;

    /**
     * 当前的bean 对象创建完毕，并且属性注入完毕
    */
    @PostConstruct
    public void init() {
        log.error("执行初始化");
        formController.throwMyException(9);
    }

    /**
     * 在上面方法后执行
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        log.error("afterPropertiesSet");
    }

    /**
     * 所有非懒加载单例bean创建完后执行
     */
    @Override
    public void afterSingletonsInstantiated() {
        log.error("所有非懒加载单例bean创建完后执行");
    }

    /**
     * 当前的容器初始化完毕，项目启动完成后的最后一步执行
     * see {@link org.springframework.boot.SpringApplication#callRunners(
     * org.springframework.context.ApplicationContext,
     * org.springframework.boot.ApplicationArguments)}
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.error("ApplicationRunner");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        log.error("获取bean 工厂");
    }
}
